#pragma once
#include "Common.hpp"
#include "InetAddr.hpp"
#include "Log.hpp"
namespace SocketModule
{
    using namespace LogModule;
    static const int gbacklog = 5;
    static const int defaultfd = -1;

    class Socket
    {
    public:
        Socket()
            : _sockfd(defaultfd)
        {
        }

        Socket(int fd)
            : _sockfd(fd)
        {
        }

        ~Socket()
        {
            if (_sockfd > 0)
            {
                close(_sockfd);
            }
        }

    public:
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        virtual std::shared_ptr<Socket> Accept(InetAddr *client) = 0;
        virtual int Read(std::string &out) = 0;
        virtual int Send(std::string &message) = 0;
        virtual int Connect(const std::string &ip, uint16_t port) = 0;
        virtual void Close() = 0;

        // 服务端初始化
        void BuildTcpSocketMethod(uint16_t port, int backlog = gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }

        // 客户端初始化
        void BuildTcpClientMethod()
        {
            SocketOrDie();
        }

        // 保护方法 子类可以访问
    protected:
        int _sockfd;
    };

    class TcpSocket : public Socket
    {
    public:
        // 使用基类的构造函数
        TcpSocket() : Socket() {}

        //  ~TcpSocket(); // 自动调用基类析构函数

    public:
        void SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::FATAL) << "socket error";
                exit(SOCKET_ERR);
            }
            LOG(LogLevel::INFO) << "socket success";
        }

        void BindOrDie(uint16_t port) override
        {
            InetAddr localaddr(port);
            int n = ::bind(_sockfd, localaddr.NetAddrPtr(), localaddr.NetAddrLen());
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "bind error";
                exit(BIND_ERR);
            }
            LOG(LogLevel::INFO) << "bind success";
        }

        void ListenOrDie(int backlog) override
        {
            int n = ::listen(_sockfd, backlog);
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "listen error";
                exit(LISTEN_ERR);
            }
            LOG(LogLevel::INFO) << "listen success";
        }

        std::shared_ptr<Socket> Accept(InetAddr *client) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int fd = ::accept(_sockfd, CONV(peer), &len);
            if (fd < 0)
            {
                LOG(LogLevel::WARNING) << "accept warning ...";
                return nullptr;
            }

            client->SetAddr(peer); // 填充客户端信息
            return std::make_shared<Socket>(fd);
        }

        int Read(std::string &out) override
        {
            char buffer[1024];
            ssize_t n = ::recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if (n > 0)
            {
                buffer[n] = 0;
                out += buffer; // 尾插
            }
            return n;
        }

        int Send(std::string &message) override
        {
            return ::send(_sockfd, message.c_str(), message.size(), 0);
        }

        int Connect(const std::string &ip, uint16_t port) override
        {
            InetAddr server_addr(ip, port); // 封装服务端信息
            return ::connect(_sockfd, server_addr.NetAddrPtr(), server_addr.NetAddrLen());
        }

        void Close() override
        {
            if (_sockfd > 0)
            {
                ::close(_sockfd);
            }
        }
    };
}